<meta charset="utf-8">
(#) Call to `SSLCertificateSocketFactory.getInsecure()`

!!! WARNING: Call to `SSLCertificateSocketFactory.getInsecure()`
   This is a warning.

Id
:   `SSLCertificateSocketFactoryGetInsecure`
Summary
:   Call to `SSLCertificateSocketFactory.getInsecure()`
Severity
:   Warning
Category
:   Security
Platform
:   Android
Vendor
:   Android Open Source Project
Feedback
:   https://issuetracker.google.com/issues/new?component=192708
Since
:   1.5.0 (November 2015)
Affects
:   Kotlin and Java files
Editing
:   This check runs on the fly in the IDE editor
See
:   https://goo.gle/SSLCertificateSocketFactoryGetInsecure
Implementation
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/SslCertificateSocketFactoryDetector.java)
Tests
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/SslCertificateSocketFactoryDetectorTest.kt)

The `SSLCertificateSocketFactory.getInsecure()` method returns an
SSLSocketFactory with all TLS/SSL security checks disabled, which could
result in insecure network traffic caused by trusting arbitrary TLS/SSL
certificates presented by peers. This method should be avoided unless
needed for a special circumstance such as debugging. Instead,
`SSLCertificateSocketFactory.getDefault()` should be used.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/test/pkg/SSLCertificateSocketFactoryTest.java:29:Warning: Use of
SSLCertificateSocketFactory.getInsecure() can cause insecure network
traffic due to trusting arbitrary TLS/SSL certificates presented by
peers [SSLCertificateSocketFactoryGetInsecure]
    SSLCertificateSocketFactory.getInsecure(-1,null));
    ------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here is the source file referenced above:

`src/test/pkg/SSLCertificateSocketFactoryTest.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
package test.pkg;

import android.net.SSLCertificateSocketFactory;
import java.net.InetAddress;
import java.net.Inet4Address;
import java.net.Inet6Address;
import javax.net.ssl.HttpsURLConnection;

public class SSLCertificateSocketFactoryTest {
    public void foo() {
        byte[] ipv4 = new byte[4];
        byte[] ipv6 = new byte[16];
        InetAddress inet = Inet4Address.getByAddress(ipv4);
        Inet4Address inet4 = (Inet4Address) Inet4Address.getByAddress(ipv4);
        Inet6Address inet6 = Inet6Address.getByAddress(null, ipv6, 0);

        SSLCertificateSocketFactory sf = (SSLCertificateSocketFactory)
        SSLCertificateSocketFactory.getDefault(0);
        sf.createSocket("www.google.com", 80); // ok
        sf.createSocket("www.google.com", 80, inet, 2000); // ok
        sf.createSocket(inet, 80);
        sf.createSocket(inet4, 80);
        sf.createSocket(inet6, 80);
        sf.createSocket(inet, 80, inet, 2000);
        sf.createSocket(inet4, 80, inet, 2000);
        sf.createSocket(inet6, 80, inet, 2000);

        HttpsURLConnection.setDefaultSSLSocketFactory(
                SSLCertificateSocketFactory.getInsecure(-1,null));
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/SslCertificateSocketFactoryDetectorTest.kt)
for the unit tests for this check to see additional scenarios.

The above example was automatically extracted from the first unit test
found for this lint check, `SslCertificateSocketFactoryDetector.test`.
To report a problem with this extracted sample, visit
https://issuetracker.google.com/issues/new?component=192708.

(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Using a suppression annotation like this on the enclosing
  element:

  ```kt
  // Kotlin
  @Suppress("SSLCertificateSocketFactoryGetInsecure")
  fun method() {
     createSocket(...)
  }
  ```

  or

  ```java
  // Java
  @SuppressWarnings("SSLCertificateSocketFactoryGetInsecure")
  void method() {
     createSocket(...);
  }
  ```

* Using a suppression comment like this on the line above:

  ```kt
  //noinspection SSLCertificateSocketFactoryGetInsecure
  problematicStatement()
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="SSLCertificateSocketFactoryGetInsecure" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'SSLCertificateSocketFactoryGetInsecure'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore SSLCertificateSocketFactoryGetInsecure ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>